package hp.com.servlet.CartDataServlet;

import hp.com.entity.Address;
import hp.com.entity.CartData;
import hp.com.entity.Users;
import hp.com.service.AddressService;
import hp.com.service.CartDataService;
import hp.com.service.impl.AddressServiceImpl;
import hp.com.service.impl.CartDataServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Checkout")
public class BuyCar_TwoShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否登录且获取对象
        HttpSession session = req.getSession();
        if (session.getAttribute("userSession") == null) {
            req.getRequestDispatcher(req.getContextPath() + "/Login").forward(req, resp);
        } else{
        try{
            req.setCharacterEncoding("utf-8");
            //获取购物车信息
            CartDataService cs = new CartDataServiceImpl();
            List<CartData> list = cs.BuyCarShow(String.valueOf(((Users)session.getAttribute("userSession")).getId()));
            req.setAttribute("cartDataList",list);
            //获取地址信息
            AddressService as = new AddressServiceImpl();
            List<Address> listAddr = as.getAddressById(((Users)session.getAttribute("userSession")).getId());
            req.setAttribute("addressList",listAddr);
            req.setAttribute("userSession",session.getAttribute("userSession"));
            //转发页面显示
            req.getRequestDispatcher(req.getContextPath()+"ShopPage/checkout.jsp").forward(req,resp);
        } catch (Exception exception) {
            exception.printStackTrace();
        }}
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
